package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.missilike.biz.IMilAccountContentService;
import com.missilike.biz.IMilContentTransmitService;
import com.missilike.core.domain.ContentRequest;
import com.missilike.core.domain.DeleteContentReuquest;
import com.missilike.core.domain.ForwardContentRequest;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.MilUserFeeds;
import com.missilike.repository.entity.MilAccountContent;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/readingArticles")
public class ReadingArticlesController {

    private final IMilAccountContentService milAccountContentService;
    private final IMilContentTransmitService transmitService;


    public ReadingArticlesController(IMilAccountContentService milAccountContentService, IMilContentTransmitService transmitService) {
        this.milAccountContentService = milAccountContentService;
        this.transmitService = transmitService;
    }

    //todo 发布长文


    @ApiOperation(value = "getFeeds", notes = "获取朋友圈信息流")
    @RequestMapping(value = "/getFeeds", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getFeeds(@RequestParam(value = "id")@ApiParam(value = "用户ID") String id, @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                 @RequestParam(value = "type",defaultValue = "1")@ApiParam(value = "1:非长文,2:长文 ") Integer type) {
        List<MilUserFeeds> feeds = milAccountContentService.getFeeds(id,lastId,pageSize,type);
        log.info("Completed execute /api/readingArticles/getFeeds responese={true}");
        return ResultUtils.ok(feeds);
    }



    @ApiOperation(value = "getUserFeeds", notes = "获取某用户朋友圈")
    @RequestMapping(value = "/getUserFeeds", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getUserFeeds(@RequestParam(value = "id")@ApiParam(value = "用户ID") String id, @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                 @RequestParam(value = "type",defaultValue = "1")@ApiParam(value = "1:非长文,2:长文 ") Integer type) {
        List<MilUserFeeds> feeds = milAccountContentService.getUserFeeds(id,lastId,pageSize,type);
        log.info("Completed execute /api/readingArticles/getFeeds responese={true}");
        return ResultUtils.ok(feeds);
    }



    @ApiOperation(value = "getFollowUsersFeeds", notes = "获取关注用户朋友圈信息流")
    @RequestMapping(value = "/getFollowUsersFeeds", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getFollowUsersFeeds(@RequestParam(value = "id")@ApiParam(value = "用户ID") String id, @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                 @RequestParam(value = "type",defaultValue = "1")@ApiParam(value = "1:非长文,2:长文 ") Integer type) {
        List<MilUserFeeds> feeds = milAccountContentService.getFollowUsersFeeds(id,lastId,pageSize,type);
        log.info("Completed execute /api/readingArticles/getFeeds responese={true}");
        return ResultUtils.ok(feeds);
    }


    @ApiOperation(value = "getUserFeedsById", notes = "获取某条朋友圈详情")
    @RequestMapping(value = "/getUserFeedsById", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getUserFeedsById(@RequestParam(value = "id")@ApiParam(value = "朋友圈") Long id,
                                         @RequestParam(value = "currentAccountId",required = false) @ApiParam(value = "当前用户Id") String currentAccountId) {
        MilUserFeeds feed = milAccountContentService.getUserFeedsById(id,currentAccountId);
        return ResultUtils.ok(feed);
    }


    //todo 申请阅读号资格

//    @ApiOperation(value = "getAccountContent", notes = "获取文章列表")
//    @RequestMapping(value = "/getAccountContent", method = {RequestMethod.GET}, produces = "application/json")
//    @ResponseBody
//    public CommonResult getAccountContent(@RequestParam Long contentId) {
//        return ResultUtils.ok(milAccountContentService.getById(contentId));
//    }

    @ApiOperation(value = "forwardContent", notes = "转发朋友圈")
    @RequestMapping(value = "/forwardContent", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult forwardContent(@RequestBody ForwardContentRequest request) {
        log.info("Start to execute /api/readingArticles/forwardContent  RequestParam ={}", JSON.toJSON(request));
        if (transmitService.forwardContent(request)) {
            log.info("Completed execute /api/readingArticles/forwardContent responese={true}");
            return ResultUtils.ok();
        } else {
            return ResultUtils.error();
        }
    }

    @ApiOperation(value = "deleteContentTransmit", notes = "删除长文或者图片转发记录")
    @RequestMapping(value = "/deleteContentTransmit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult deleteContentTransmit(@RequestBody DeleteContentReuquest reuquest) {
        log.info("Start to execute /api/readingArticles/deleteContentTransmit  RequestParam ={}", JSON.toJSON(reuquest));
        if (transmitService.deleteContentTransmit(reuquest)) {
            log.info("Completed execute /api/readingArticles/deleteContentTransmit responese={true}");
            return ResultUtils.ok();
        } else {
            return ResultUtils.error();
        }
    }


    @ApiOperation(value = "sendContent", notes = "发布朋友圈")
    @RequestMapping(value = "/sendContent", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult sendContent(@RequestBody ContentRequest request) {
        log.info("Start to execute /api/readingArticles/sendContent  RequestParam ={}", JSON.toJSON(request));
        CommonResult result = milAccountContentService.sendContent(request);
        log.info("Completed execute /api/readingArticles/sendContent responese={}", JSON.toJSON(result));
        return result;
    }


}
